package com.hsm.controller;

import com.hsm.constant.MessageConstant;
import com.hsm.entity.PageResult;
import com.hsm.entity.QueryPageBean;
import com.hsm.entity.Result;
import com.hsm.pojo.CheckGroup;
import com.hsm.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查项
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(Integer[] checkItemsIds, @RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.add(checkItemsIds,checkGroup);

            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }


    }

    /**
     * 分页查询检查组信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping(value = "/findPage",method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return pageResult;

    }


    /**
     * 删除检查项
     * @return
     */
    @RequestMapping(value = "/deleteCheckGroup",method = RequestMethod.GET)
    public Result deleteCheckGroup(Integer checkGroupId){
        try {
            checkGroupService.deleteCheckGroup(checkGroupId);

            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }


    }

    /**
     * 根据id查询检查组信息  进行数据回显
     * @param checkGroupId
     * @return
     */
    @RequestMapping(value = "/findCheckGroupById",method = RequestMethod.POST)
    public Result findCheckGroupById(Integer checkGroupId){
        Result result = null;
        try {
            CheckGroup checkGroup = checkGroupService.findCheckGroupById(checkGroupId);
            result = new Result(true,"数据回显成功",checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false,"数据回显失败");
        }
        return result;
    }

    /**
     * 通过检查组id查询中间表t_checkgroup_checkitem中 有多少个检查项
     */
    @RequestMapping(value = "/findCheckItemIdsByCheckGroupId",method = RequestMethod.POST)
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id){
        List<Integer> list = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return list;
    }

    /**
     * 修改检查组信息
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        Result result = null;
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
            result = new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return result;
    }

    /**
     * 查询所有检查组信息
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        Result result = null;
        try {
            List<CheckGroup> list = checkGroupService.findAll();
            result = new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return result;
    }

}
